local yajiao = fk.CreateSkill {

  name = "joyex__yajiao",

  tags = {  },

}



yajiao:addEffect(fk.CardUsing, {
  name = "joyex__yajiao",
  anim_type = "control",
  events = {fk.CardUsing, fk.CardResponding, fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(yajiao.name) and target == player then
      if event == fk.EventPhaseStart then
        return player.phase == Player.Finish and player:usedSkillTimes("joyex__longdan", Player.HistoryTurn) > 0
      else
        return U.IsUsingHandcard(player, data) and player ~= player.room.current
      end
    end
  end,
  on_cost = function(self,event,target,player,data)
    if event == fk.EventPhaseStart then
      return true
    else
      return player.room:askForSkillInvoke(player,yajiao.name,data)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      player:drawCards(1 ,yajiao.name)
    else
      local cards = room:getNCards(1)
      player:showCards(cards)
      local tos = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1, "#joyex__yajiao-choose:::"..Fk:getCardById(cards[1]):toLogString(), yajiao.name, false)
      if #tos > 0 then
        local to = room:getPlayerById(tos[1])
        room:moveCardTo(cards,Card.PlayerHand,to,fk.ReasonGive,yajiao.name,nil,true,player)
      end
    end
  end,
})
yajiao:addEffect(fk.CardResponding, {
  name = "joyex__yajiao",
  anim_type = "control",
  events = {fk.CardUsing, fk.CardResponding, fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(yajiao.name) and target == player then
      if event == fk.EventPhaseStart then
        return player.phase == Player.Finish and player:usedSkillTimes("joyex__longdan", Player.HistoryTurn) > 0
      else
        return U.IsUsingHandcard(player, data) and player ~= player.room.current
      end
    end
  end,
  on_cost = function(self,event,target,player,data)
    if event == fk.EventPhaseStart then
      return true
    else
      return player.room:askForSkillInvoke(player,yajiao.name,data)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      player:drawCards(1 ,yajiao.name)
    else
      local cards = room:getNCards(1)
      player:showCards(cards)
      local tos = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1, "#joyex__yajiao-choose:::"..Fk:getCardById(cards[1]):toLogString(), yajiao.name, false)
      if #tos > 0 then
        local to = room:getPlayerById(tos[1])
        room:moveCardTo(cards,Card.PlayerHand,to,fk.ReasonGive,yajiao.name,nil,true,player)
      end
    end
  end,
})
yajiao:addEffect(fk.EventPhaseStart, {
  name = "joyex__yajiao",
  anim_type = "control",
  events = {fk.CardUsing, fk.CardResponding, fk.EventPhaseStart},
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(yajiao.name) and target == player then
      if event == fk.EventPhaseStart then
        return player.phase == Player.Finish and player:usedSkillTimes("joyex__longdan", Player.HistoryTurn) > 0
      else
        return U.IsUsingHandcard(player, data) and player ~= player.room.current
      end
    end
  end,
  on_cost = function(self,event,target,player,data)
    if event == fk.EventPhaseStart then
      return true
    else
      return player.room:askForSkillInvoke(player,yajiao.name,data)
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    if event == fk.EventPhaseStart then
      player:drawCards(1 ,yajiao.name)
    else
      local cards = room:getNCards(1)
      player:showCards(cards)
      local tos = room:askForChoosePlayers(player, table.map(room.alive_players, Util.IdMapper), 1, 1, "#joyex__yajiao-choose:::"..Fk:getCardById(cards[1]):toLogString(), yajiao.name, false)
      if #tos > 0 then
        local to = room:getPlayerById(tos[1])
        room:moveCardTo(cards,Card.PlayerHand,to,fk.ReasonGive,yajiao.name,nil,true,player)
      end
    end
  end,
})

return yajiao